package com.ostrich.note.ui.note.views;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.SDCardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseActivity;
import com.ostrich.note.databinding.ActivityImageDetailBinding;
import com.ostrich.note.ui.note.entity.NoteEntity;
import com.ostrich.note.utils.SDUtils;

import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;


import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ImageDetailActivity extends BaseActivity<ActivityImageDetailBinding> {
    private NoteEntity note;
    private int total;
    private int current;
    private ArrayList<String> list = new ArrayList<>();

    @Override
    protected void iniView() {
        note = (NoteEntity) getBundle().getSerializable("note");
        String[] split = note.getThumb().split(",");
        total = split.length;
        showText();


        for (String s : split) {
            list.add(s);
        }

        binding.banner.setAdapter(new ImageAdapter(list));
        binding.banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                showText();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });




    }

    public void showText(){
        StringBuilder stringBuilder = new StringBuilder().append("(")
                .append(current + 1)
                .append("/")
                .append(total)
                .append(")");
        binding.textView.setText(stringBuilder.toString());
    }

    @Override
    public int getLayout() {
        return R.layout.activity_image_detail;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        binding.clBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.ivDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(list.get(current)));
                request.setDestinationInExternalPublicDir(  Environment.DIRECTORY_DOWNLOADS  , System.currentTimeMillis()+".jpg" ) ;
                downloadManager.enqueue(request);
                ToastUtils.showShort("下载成功");
//每下载的一个文件对应一个id，通过此id可以查询数据。
            }
        });

    }

    public class ImageAdapter extends BannerAdapter<String, ImageAdapter.BannerViewHolder> {

        public ImageAdapter(List<String> mDatas) {
            //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
            super(mDatas);
        }

        //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
        @Override
        public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(parent.getContext());
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return new BannerViewHolder(imageView);
        }

        @Override
        public void onBindView(BannerViewHolder holder, String data, int position, int size) {
            Glide.with(holder.imageView).load(data).transform(new CenterCrop()).into(holder.imageView);
        }

        class BannerViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public BannerViewHolder(@NonNull ImageView view) {
                super(view);
                this.imageView = view;
            }
        }
    }

}